package com.example.demo01minio;

import com.example.demo01minio.config.MinioProperties;
import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.SetBucketPolicyArgs;
import io.minio.errors.*;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;


// Generated by https://start.springboot.io
// 优质的 spring/boot/data/security/cloud 框架中文文档尽在 => https://springdoc.cn
@SpringBootApplication
@MapperScan("com.example.demo01minio.mapper")
public class Demo01MinioApplication {

    public static void main(String[] args) {
        SpringApplication.run(Demo01MinioApplication.class, args);
    }

    @Bean
    public MinioClient getMinioClient() throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        String bucket = MinioProperties.BUCKET;
        //Builder构建者设计模式
        MinioClient client = MinioClient.builder()
                .credentials(MinioProperties.ACCESS_KEY, MinioProperties.SECRET_KEY)
                .endpoint(MinioProperties.ENDPOINT)
                .build();

        boolean exists = client.bucketExists(BucketExistsArgs.builder()
                .bucket(bucket)
                .build());
        if (!exists) {
            //桶不存在，创建桶设置权限
            client.makeBucket(MakeBucketArgs.builder()
                    .bucket(bucket)
                    .build());

            String policyJson = "{\n" +
                    "                        \"Version\": \"2012-10-17\",\n" +
                    "                        \"Statement\": [\n" +
                    "                            {\n" +
                    "                                \"Effect\": \"Allow\",\n" +
                    "                                \"Principal\": {\n" +
                    "                                    \"AWS\": [\n" +
                    "                                        \"*\"\n" +
                    "                                    ]\n" +
                    "                                },\n" +
                    "                                \"Action\": [\n" +
                    "                                    \"s3:GetBucketLocation\",\n" +
                    "                                    \"s3:ListBucket\",\n" +
                    "                                    \"s3:ListBucketMultipartUploads\"\n" +
                    "                                ],\n" +
                    "                                \"Resource\": [\n" +
                    "                                    \"arn:aws:s3:::" + bucket + "\"\n" +
                    "                                ]\n" +
                    "                            },\n" +
                    "                            {\n" +
                    "                                \"Effect\": \"Allow\",\n" +
                    "                                \"Principal\": {\n" +
                    "                                    \"AWS\": [\n" +
                    "                                        \"*\"\n" +
                    "                                    ]\n" +
                    "                                },\n" +
                    "                                \"Action\": [\n" +
                    "                                    \"s3:AbortMultipartUpload\",\n" +
                    "                                    \"s3:DeleteObject\",\n" +
                    "                                    \"s3:GetObject\",\n" +
                    "                                     \"s3:ListMultipartUploadParts\",\n" +
                    "                                    \"s3:PutObject\"\n" +
                    "                                ],\n" +
                    "                                \"Resource\": [\n" +
                    "                                    \"arn:aws:s3:::" + bucket + "/*\"\n" +
                    "                                ]\n" +
                    "                            }\n" +
                    "                        ]\n" +
                    "                    }";
            //给桶设置全选
            client.setBucketPolicy(SetBucketPolicyArgs.builder()
                    .bucket(bucket)
                    .config(policyJson)
                    .build());
        }
        return client;
    }
}
